import { SpriteFrame } from "cc";
import WorldManager from "../common/WorldManager";
import { MapZone } from "./MapZone";
import ResManager from "../common/ResManager";

export class Village {
   /**名字 */
    name:string;
    /**区块id */
    zoneId:number;
    /**资源田的类型 */
    typeId:number;
    uid:number;
    x:number;
    y:number;
    people:number;
    race:number
    user:any
    /**当前村的zoneId */
    curVid:number
    /**主村的zoneId */
    curVillage:number
   

    isMainVillage:boolean
   

    /**
     * 根据服务端的数据，来创建村庄对象
     * @param obj 
     * @returns 
     */
    static createByVillageObj(obj): Village{

        let v = new Village()
        for(let key in obj){
            v[key] = obj[key]
        }
        v.name = obj.villageName
        

        let coord = MapZone.getXyByZoneId(v.zoneId, WorldManager.instance.radius)
        v.x = coord[0]
        v.y = coord[1]
        return v
    }

    async getImg():Promise<SpriteFrame>{
        let race = this.race
        if(race == null){
            race = this.user.uInfo.race
        }
        let level:number = 1

        if(this.people <= 100){
            level = 1
        } 
        if(100 < this.people && this.people <= 500){
            level = 2
        }

        if(500 < this.people && this.people <= 1000){
            level = 3
        }

        if(this.people > 1000){
            level = 4
        }
        let sf
        try {
             sf = await ResManager.instance.loadAssetByUrl<SpriteFrame>(`zoneType/city${race}-${level}/spriteFrame`)
        } catch (error) {
           
        }

        return sf
    }

    /**
     * 判断注册时间是否超过3天
     */
    get isOverThreeDay():boolean{
        if(!this.user.regTime){
            return true
        }

        let now = new Date().getTime() / 1000
        
        let day = (now - this.user.regTime) / 3600 / 24
        return day > 3
    } 
}


